#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 28;
use lib 'tests/search';
use AIR2TestUtils;    # sets lib path
use AIR2::Searcher::Sources;
use AIR2::Searcher::Sources::Engine;
use Data::Dump qw( dump );
use JSON::XS;

SKIP: {

    if ( !AIR2TestUtils::search_env_ok() ) {
        skip "The search env does not look sane. Skipping all tests", 28;
    }

    my $authz = AIR2TestUtils::dummy_authz();

    ok( my $searcher = AIR2::Searcher::Sources->new(), "new searcher" );

    ok( my $results = $searcher->do_search(
            {   q        => 'oil spill',
                auth_tkt => $authz,
                p        => 3,             # small for speed
            }
        ),
        "get results"
    );

    #diag( dump $results );
    ok( exists $results->{unauthz_total}, "unauthz_total present" );

#diag( "total=" . $results->{total} . " unauthz_total=" . $results->{unauthz_total} );
    cmp_ok(
        $results->{total}, '<',
        $results->{unauthz_total},
        "unauthz_total > total"
    );

    my @expected_fields = qw(
        primary_location
        primary_email
        primary_phone
        excerpts
        title
        uri
        summary
        qa
    );

    for my $res ( @{ $results->{results} } ) {

        for my $f (@expected_fields) {
            ok( defined $res->{$f}, "$f defined" );

        }
    }

}
